<?PHP
	require 'includes/master.inc.php';
	require 'includes/ap.inc.php';

	if(count($_POST) == 0) die('nope');

	// Do PayPal post back
	$arr = array();
	foreach($_POST as $k => $v)
		$arr[] = $k . "=" . urlencode($v);
	$str = implode('&', $arr) . '&cmd=_notify-validate';
	$response = curl(get_option('paypal_url', DEFAULT_PAYPAL_URL), null, $str);
	// $response = curl('http://www.eliteweaver.co.uk/cgi-bin/webscr', null, $str);

	// Check response
	if(eregi('VERIFIED', $response) && ($_POST['payment_status'] == "Completed"))
	{
		// TODO: We need to send an email if the app isn't found.
		$app = new App();
		$app->select($_POST['item_name'], 'name');
		if($app->id == '') error_log('App not found!');

		$o = new Order();
		$o->postLoad();
		$o->type   = "PayPal";
		$o->app_id = $app->id;
		$o->dt     = dater("Y-m-d H:i:s", $_POST['payment_date']);
		$o->insert();
		$o->generateLicense();
		$o->emailLicense();
	}
	
